package com.cmc.cloud.cmclink.doc.controller.admin.so;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.DgDescErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.service.so.DgDescService;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.dginfovo.DgDescUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.convert.DgDescConvert;
import com.cmc.cloud.cmclink.doc.entity.DgDescDO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 危险品信息补充前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 天津危险品信息维护")
@RestController
@Validated
@Slf4j
@RequestMapping(UrlConstants.APPLICATION_URL + "/dg-info-supplement")
public class DgDescController {

    @Resource
    private DgDescService dgDescService;

    @Operation(summary = "创建危险品信息补充")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:DGBasicInfoMaintain:add')")
    public CommonResult<Void> createDgDesc(@Valid @RequestBody DgDescCreateReqVO createReqVO) {
        dgDescService.createDgDesc(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新危险品信息补充")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:DGBasicInfoMaintain:edit')")
    public CommonResult<Void> updateDgDesc(@Valid @RequestBody DgDescUpdateReqVO updateReqVO) {
        dgDescService.updateDgDesc(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除危险品信息补充")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:DGBasicInfoMaintain:delete')")
    public CommonResult<Void> deleteDgDesc(@PathVariable("id") Long id) {
        dgDescService.deleteDgDesc(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得危险品信息补充")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:DGBasicInfoMaintain:search')")
    public CommonResult<DgDescRespVO> getDgDesc(@PathVariable("id") Long id) {
        DgDescDO dgDesc = dgDescService.getDgDesc(id);
        return CommonResult.success(DgDescConvert.INSTANCE.convert(dgDesc));
    }

    @Operation(summary = "获得危险品信息补充列表")
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @UseResponsePropertyAdvice
    public CommonResult<List<DgDescRespVO>> getDgDescList(@RequestParam("ids") Collection<Long> ids) {
        List<DgDescDO> list = dgDescService.getDgDescList(ids);
        return CommonResult.success(DgDescConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得危险品信息补充分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<DgDescRespVO>> getDgDescPage(@Valid @RequestBody DgDescPageReqVO pageVO) {
        PageResult<DgDescDO> pageResult;
        try {
            pageResult = dgDescService.getDgDescPage(pageVO);
        } catch (Exception e) {
            log.error("caught an exception on getDgDescPage", e);
            throw ServiceExceptionUtil.exception(DgDescErrorCodeConstants.DG_SQL_FAILED);
        }
        List<DgDescRespVO> results = DgDescConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
